package com.service.Impl;

import com.dao.UserDao;
import com.dao.UserRoleDao;
import com.pojo.Role;
import com.pojo.User;
import com.pojo.UserRole;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;
//表明这个是一个ioc容器管理的bean
@Service
public class UserServiceImpl implements UserService {
    private UserDao userDao;
    private UserRoleDao userRoleDao;

//实列化，通过有参的构造方法进行传值，可以自动生成
    @Autowired
    public UserServiceImpl(UserDao userDao, UserRoleDao userRoleDao) {
        this.userDao = userDao;
        this.userRoleDao = userRoleDao;
    }

    @Override
    public Set<User> getAll() {
        return userDao.queryAll();
    }

    @Override
    public boolean addOne(User user) {
        int r=userDao.insertByUser(user);
        return r>0;//表示更新了多少个
    }

    @Override
    public boolean removeOne(User user) {
        int r = userDao.deleteByUser(user);
        return r>0;
    }

    @Override
    public boolean modifyOne(User user) {
        int r = userDao.updateByUser(user);
        return r>0;
    }

    @Override
    public Set<User> getAllRole() {
        return userDao.queryAllRole();
    }

    @Override
    public Set<User> getAllRoles() {
        return userDao.queryAllRoles();
    }
//添加用户的同时还添加了角色的方法
    @Override
    public boolean addM(User user) {
        int r=userDao.insertByUserRole(user);
        //进行判断,不能为空
        if (user.getRoles() !=null){
            //下面的代码表达是，添加了一个用户，如果你用户里面有角色那么我们就将这个关联到user_role表里面去
            for (Role role : user.getRoles()){
                UserRole userRole  = new UserRole();
                userRole.setRoleId(role.getRoleId());
                userRole.setUserId(user.getUserId());
                userRoleDao.insert(userRole);
            }
        }
        return r>0;
    }
//通过三个条件进行模糊查询
    @Override
    public Set<User> querySan(String userName, String userPhone, String roleName) {
        return userDao.queryNamePhoneRole(userName,userPhone,roleName);
    }

    //通过电话号码进行模糊查询
    @Override
    public Set<User> queryThatAddressLike1(String phone) {
        String b = "phone";
        return userDao.queryByColumn(b,phone);
    }

    //通过地址进行模糊查询
    @Override
    public Set<User> queryThatAddressLike(String address) {
        String c = "address";
        return userDao.queryByColumn(c,address);
    }

    @Override
    public boolean removeSome(Set<Integer> userIds) {
        int re = userDao.deletedIds(userIds);
        return re > 0;
    }
}
